package com.smit.livevideo.sample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.smit.adapter.DVBAdapter;
import com.smit.livevideo.sample.activity.LauncherActivity;
import com.smit.utils.LogUtil;

import com.smit.livevideo.sample.util.ShareValueUtil;

/**
 * Created by smfang on 2015/12/7.
 * 用来监听开机启动融合电视广播
 */
public class BootReceiver extends BroadcastReceiver
{
    private static final String TAG = BootReceiver.class.getSimpleName();
    private static final String BOOT_COMPELED = "android.intent.action.BOOT_COMPLETED";
    SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        LogUtil.trace(TAG, "BootReceiver onReceive");
        String action = intent.getAction();
        if (BOOT_COMPELED.equals(action))
        {
            preferences = context.getSharedPreferences(CommonData.PREF_SHARE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(CommonData.PREF_BOOT_COMPLETED, 1);
            editor.apply();

            int bootMode = ShareValueUtil.getShareIntValue(CommonData.PREF_USER_BOOT_MODE, -1);
            if (bootMode == 1 && DVBAdapter.getInstance().getDVBDeviceManager().getDVBDevice().isConnected())
            { // 0:自动启动 1:手动启动
                Intent intent2 = new Intent(context, LauncherActivity.class);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent2);
            }
        }
    }
}